<template>
	<view class="page_">
		<pageheader :setting="settingdata"></pageheader>
		<view class="contents">
			<block v-for="(item,index) in list" :key="index" v-if="(isshow&&index>3)||index<=3">
				<view class="form_one">
					<view class="title">{{item.title}} <text class="required" v-if="item.rule=='required'">*</text>
					</view>
					<block v-if="item.type=='text'||item.type=='string'">
						<view class="form_rights">
							<u-input v-model="item.value" :placeholder="'请输入'+item.title" type="text" :border="true"
								:clearable="false" />
						</view>
					</block>
					<block v-if="item.type=='number'">
						<view class="form_rights">
							<u-input v-model="item.value" :placeholder="'请输入'+item.title" type="number" :border="true"
								:clearable="false" />
						</view>
					</block>
					<picker class="form_rights" @change="setunit" :data-index="index" :value="item.arraysindex"
						:range="item.contents" v-if="item.type=='select'">
						<view class="select">
							<view class="value">{{item.contents[item.arraysindex]}}</view>
							<u-icon name="arrow-down-fill" size="26" color='#c0c4cc;'></u-icon>
						</view>
					</picker>
					<block v-if="item.type=='screen'">
						<view class="form_rights">
							<view class="rightsbox">
								<u-input v-model="item.value1" :placeholder="item.value1>0?item.value1:(item.value2>0?item.value2:item.contents[0])" input-align="right" type="number" :border="true" style="width:220rpx;" :clearable="false" />
								<view class="fengexian">——</view>
								<u-input v-model="item.value2" :placeholder="item.value2>0?item.value2:(item.value1>0?item.value1:item.contents[1])" input-align="right" type="number" :border="true" style="width:220rpx;" :clearable="false" />
							</view>
						</view>
					</block>
				</view>
			</block>
			<view class="textbtn" @click="isshowlist"><u-icon :label="isshowtxt" label-pos="left" :name="isshowstyle"></u-icon></view>
		</view>
		<view class="btns">
			<button class='confirm' @click="submit">确认</button>
		</view>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				settingdata: {
					name: '货品属性',
					bgColor: '#fff',
					titleColor: '#000000',
					showNav: true,
					showTypes: 0,
					position: "choosesku"
				},
				form: {},
				cate_id: 0,
				list: [],
				isshow:false,
				isshowtxt:'更多参数',
				isshowstyle:'arrow-down-fill'
			}
		},
		onLoad(options) {
			_this = this
			_this.cate_id = options.id
			_this.getdata()
		},
		onShow() {},
		methods: {
			isshowlist(){
				if(_this.isshow){
					_this.isshow=false;
					_this.isshowtxt='更多参数';
					_this.isshowstyle='arrow-down-fill';
				}else{
					_this.isshow=true;
					_this.isshowtxt='收起';
					_this.isshowstyle='arrow-up-fill';
				}
				
			},
			// 获取数据
			getdata() {
				_this.$api.get("getspeclist", {
					cate_id: _this.cate_id
				}).then(data => {
					data.forEach((item, index) => {
						item.arraysindex = 0
						item.value = ""
						item.value1 = ""
						item.value2 = ""
						_this.list.push(item)
					})
				});
			},
			setunit(e) {
				let index = e.currentTarget.dataset.index
				let arraysindex = e.detail.value
				_this.list[index].arraysindex = arraysindex
			},
			submit() {
				let list = _this.list
				// 验证
				let verify = []
				// 存储
				let data = []
				list.forEach((item, index) => {
					if (item.type == "screen") {
						item.value1 = item.value1 > 0 ? item.value1 : (item.value2 > 0 ? item.value2 : "")
						item.value2 = item.value2 > 0 ? item.value2 : (item.value1 > 0 ? item.value1 : "")
					}
					if (item.rule == "required") {
						if (item.type == "text" || item.type == "text" || item.type == "number") {
							verify.push({
								value: item.value,
								type: 1,
								title: "请输入" + item.title
							})
						} else if (item.type == "screen") {
							verify.push({
								value: item.value1,
								type: 1,
								title: "请输入" + item.title
							}, {
								value: item.value2,
								type: 1,
								title: "请输入" + item.title
							}, {
								value: [item.value1, item.value2],
								type: 7,
								title: item.title + "尺寸有误"
							})
						}
						// }else{
						// 	if(item.type=="screen"&&(item.value1.length>0||item.value2.length>0)){
						// 		verify.push({
						// 		value: item.value1,
						// 		type: 1,
						// 		title: "请输入"+item.title
						// 		},{
						// 		value: item.value2,
						// 		type: 1,
						// 		title: "请输入"+item.title
						// 		},{
						// 		value: [item.value1,item.value2],
						// 		type: 7,
						// 		title: item.title+"尺寸有误"
						// 		})
						// 	}
					}
					if (item.type == "text" || item.type == "text" || item.type == "number") {
						if (item.value.length > 0 || item.rule == "required") {
							data.push([item.title] + ":" + item.value)
						}
					} else if (item.type == "screen") {
						item.value1 = item.value1 > 0 ? item.value1 : (item.value2 > 0 ? item.value2 : "")
						item.value2 = item.value2 > 0 ? item.value2 : (item.value1 > 0 ? item.value1 : "")
						if (item.value1.length > 0 || item.value2.length > 0 || item.rule == "required") {
							data.push([item.title] + ":" + (item.value1 + '-' + item.value2))
						}
					} else if (item.type == "select") {
						data.push([item.title] + ":" + item.contents[item.arraysindex])
					}
				})
				if (_this.$plug.verify(verify)) {
					uni.setStorageSync("spec", data.join(','))
					wx.navigateBack({
						delta: 1
					})
				}
			},

		}
	}
</script>

<style lang="scss" scoped>
	.page_ {
		height: 100vh;
		display: flex;
		flex-direction: column;

		.contents {
			flex: 1;
			border-top: 10rpx solid #f2f2f2;
			overflow-y: auto;
			padding: 0 46rpx 0 31rpx;

			.form_one {
				display: flex;
				flex-direction: row;
				margin-top: 36rpx;

				.title {
					color: #222222;
					font-size: 30rpx;
					font-weight: bold;
					margin-top: 20rpx;
					min-width: 160rpx;

					.required {
						color: #F00;
						margin-left: 6rpx;
					}
				}

				.form_rights {
					margin-left: 20rpx;
					flex: 1;
					display: flex;
					flex-direction: column;
					width: 100%;

					.select {
						display: flex;
						width: 100%;
						border: 2rpx solid #dcdfe6;
						padding: 0 20rpx;
						justify-content: space-between;
						align-items: center;
						font-size: 28rpx;
						border-radius: 6rpx;
						line-height: 70rpx;
					}

					.rightsbox {
						display: flex;
						flex-direction: row;
						align-items: center;

						.fengexian {
							margin: 0 8rpx;
							color: #E6E6E6;
							font-size: 22rpx;
						}
					}
				}
			}
		}

		.btns {
			height: 178rpx;
			display: flex;
			flex-direction: row;
			background-color: #fff;
			justify-content: center;
			align-items: center;

			.confirm {
				width: 680rpx;
				background-color: #38BE8C;
				line-height: 90rpx;
				text-align: center;
				color: #fff;
				font-size: 34rpx;
				font-weight: bold;
				height: 90rpx;
				border-radius: 45rpx;
			}
		}
	}
	.textbtn{
		width: 160rpx;
		background: #E6E6E6;
		color: #000;
		font-size: 30rpx;
		padding: 10rpx;
		flex: 1;
		margin-left: auto;
		margin-top: 20rpx;
		text-align: center;
		justify-content: center; // 内容自适应：上下居中
	}
	.redOne {
	            width: 0;
	            height: 0;
	            border-left: 10rpx solid transparent;
	            border-right: 10rpx solid transparent;
	            border-bottom: 20rpx solid #333;
	        }
	 
	        .greenOne {
	            width: 0;
	            height: 0;
	            border-left: 10rpx solid transparent;
	            border-right: 10rpx solid transparent;
	            border-top: 20rpx solid #333;
	        }

</style>
